﻿using System;
using System.Runtime.Serialization;

namespace WickedSick.ClientComponents.Music
{
    public enum SongLocations
    {
        Archive = 0,
        Library = 1
    }

    [Serializable]
    public class Song : ISerializable
    {
        private int _ID;
        private Artist _ParentArtist;
        private Album _ParentAlbum;
        private string _Title;
        private Int16 _TrackNumber;
        private TimeSpan _Duration;
        private SongLocations _SongLocation;
        private DateTime _UploadDate;

        public Song()
        {
        }

        /// <summary>
        /// Deserializable constructor
        /// </summary>
        /// <param name="info"></param>
        /// <param name="ctxt"></param>
        public Song(SerializationInfo info, StreamingContext context)
        {
            this.ID = info.GetInt32("ID");
            this.Title = info.GetString("Title");
            this.TrackNumber = info.GetInt16("TrackNumber");
            this.Duration = (TimeSpan)info.GetValue("Duration", typeof(TimeSpan));
            this.SongLocation = (SongLocations)info.GetValue("SongLocation", typeof(SongLocations));
            this.UploadDate = info.GetDateTime("UploadDate");
        }
        
        public Song(int id, string new_song)
        {
            string delim = "<|>";
            string delim2 = ">|<";
            //[Location]<|>[Title]<|>[Track Number]<|>[Upload Date]<|>[Duration]>|<[Parent Artist ID]>|<[Parent Album ID]
            string[] tokens = new_song.Remove(new_song.IndexOf(delim2)).Split(new string[] {delim}, StringSplitOptions.None);

            if (tokens.Length != 5)
                throw new Exception("Invalid song protocol");

            this.ID = id;
            this.SongLocation = (SongLocations)Convert.ToInt32(tokens[0]);
            this.Title = tokens[1];
            this.TrackNumber = Convert.ToInt16(tokens[2]);
            this.UploadDate = Convert.ToDateTime(tokens[3]);
            this.Duration = TimeSpan.FromTicks(Convert.ToInt64(tokens[4]));
        }

        public int ID
        {
            get { return _ID; }
            set { _ID = value; }
        }

        public Artist ParentArtist
        {
            get { return _ParentArtist; }
            set { _ParentArtist = value; }
        }

        public Album ParentAlbum
        {
            get { return _ParentAlbum; }
            set { _ParentAlbum = value; }
        }

        public string Title
        {
            get { return _Title; }
            set { _Title = value; }
        }

        public Int16 TrackNumber
        {
            get { return _TrackNumber; }
            set { _TrackNumber = value; }
        }

        public TimeSpan Duration
        {
            get { return _Duration; }
            set { _Duration = value; }
        }

        public SongLocations SongLocation
        {
            get { return _SongLocation; }
            set { _SongLocation = value; }
        }

        public DateTime UploadDate
        {
            get { return _UploadDate; }
            set { _UploadDate = value; }
        }

        public void ModifyField(string field_name, string new_value)
        {
            if (field_name == "Title")
            {
                Title = new_value;
            }
            else if (field_name == "TrackNumber")
            {
                TrackNumber = short.Parse(new_value);
            }
            else if (field_name == "Duration")
            {
                Duration = TimeSpan.Parse(new_value);
            }
            else if (field_name == "SongLocation")
            {
                SongLocation = (SongLocations)int.Parse(new_value);
            }
            else if (field_name == "UploadDate")
            {
                UploadDate = DateTime.Parse(new_value);
            }
        }

        #region ISerializable Members

        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("ID", this.ID);
            info.AddValue("Title", this.Title);
            info.AddValue("TrackNumber", this.TrackNumber);
            info.AddValue("Duration", this.Duration);
            info.AddValue("SongLocation", this.SongLocation);
            info.AddValue("UploadDate", this.UploadDate);
        }

        #endregion
    }
}
